﻿package batchload;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class TestBatchLoad002 {
    
    Connection          conn            = null;
    PreparedStatement   statement       = null;    
    static int          TEST_VALUE      ;          
    
    //기본생성자
    public TestBatchLoad002(){
        TEST_VALUE = 1000;
    }
    
    //DB접속
    public void DBConn(){
        try {
            //Sybase ASE
            Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
            conn = DriverManager.getConnection( "jdbc:sybase:Tds:58.76.19.47:5000/DMASIASE", "aisase","aisase");
            
            //Sybase IQ
            //Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
            //conn = DriverManager.getConnection( "jdbc:sybase:Tds:58.76.19.47:2361", "aisiq","aisiq");

            //ORACLE
            //Class.forName("oracle.jdbc.driver.OracleDriver");
            //conn = DriverManager.getConnection("jdbc:oracle:thin:@58.76.19.148:1522:ORCL","scott","tiger");
            
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //입력값
    public void ValueSet(){
        
        do{
            System.out.print("테스트 몇회수행?? [숫자를 넣으세요]: ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int input_int;
            
            try {
                input_int = Integer.parseInt(br.readLine().trim());
            } catch (Exception e) {
                continue;
            }
            TEST_VALUE = input_int;
            break;
        }while(true) ;
    }
    
    //batch수행
    public void excute(){
        
        try {
            conn.setAutoCommit(false);
            statement = conn.prepareStatement(
                    "INSERT INTO TABLEX3 VALUES"
                    + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//10
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//20
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//30
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//40
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//50
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//60
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//70
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//80
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//90
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//100
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//110
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//120
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//130
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//140
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//150
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//160
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//170
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//180
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//190
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"//200
                    );
            
            long stime = System.currentTimeMillis(); //시작시간
            
            statement  = setting(statement);
            
            int [] counts = statement.executeBatch();
            conn.commit();
            
            long etime  = System.currentTimeMillis();
            System.out.println("Total ruunig time : " + (etime - stime) + "ms"); 
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("SQL error");
        } 
    }
    
    public PreparedStatement setting(PreparedStatement statement) throws SQLException{
        
        for(int i = 0 ; i < TEST_VALUE ; i++){
            statement.clearParameters();
            statement.setInt(1, i);
            
            for(int j = 2 ; j < 201 ; j++){
                statement.setString(j, "JunSeok!!");    
            }
            statement.addBatch();
        }
        
        return statement;
    }
    
    //건by건
    public void excute2(){
        
        try {
            statement = conn.prepareStatement(
                    "INSERT INTO TABLEX3 VALUES"
                    + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//10
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//20
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//30
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//40
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//50
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//60
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//70
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//80
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//90
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//100
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//110
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//120
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//130
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//140
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//150
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//160
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//170
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//180
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?"//190
                    + ",?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"//200
                    );
            
            long stime = System.currentTimeMillis(); //시작시간
            
            for(int i = 0 ; i < TEST_VALUE ; i++){
                statement.clearParameters();
                statement.setInt(1, i);
                
                for(int j = 2 ; j < 201 ; j++){
                    statement.setString(j, "JunSeok!!");    
                }
                
                statement.executeUpdate();
            }
            
            long etime  = System.currentTimeMillis();
            System.out.println("Total ruunig time : " + (etime - stime) + "ms");
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("SQL error");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("error");
        }
    }
    
//    public static void main(String[] args){
//        
//        TestBatchLoad002 ob = new TestBatchLoad002();
//        ob.DBConn();
//        
//        ob.ValueSet();
//        
//        System.out.println("batch수행");
//        ob.excute();
//        
//        System.out.println("건by건");
//        ob.excute2();
//        
//    }

}
